// src/common/TDate.ts
import { CreateDateColumn, UpdateDateColumn } from 'typeorm'

export function TDate() {
  return function (target: Function) {
    // 添加 create_time 字段
    Object.defineProperty(target.prototype, 'create_time', {
      get: function () {
        return this.createTime
      },
      set: function (value) {
        this.createTime = value
      },
      configurable: true,
      enumerable: true
    })

    // 添加 update_time 字段
    Object.defineProperty(target.prototype, 'update_time', {
      get: function () {
        return this.updateTime
      },
      set: function (value) {
        this.updateTime = value
      },
      configurable: true,
      enumerable: true
    })

    // 使用 TypeORM 的装饰器来定义列
    CreateDateColumn({
      type: 'timestamp',
      nullable: false,
      name: 'create_time'
    })(target.prototype, 'createTime')
    UpdateDateColumn({
      type: 'timestamp',
      nullable: false,
      name: 'update_time'
    })(target.prototype, 'updateTime')
  }
}
